home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / share / perl5 / HTTP / Daemon.pm < prev    next >
Encoding:
Perl POD Document  |  2004-12-11  |  21.7 KB  |  860 lines

  1. package HTTP::Daemon;
  2.  
  3. # $Id: Daemon.pm,v 1.36 2004/12/11 14:13:16 gisle Exp $
  4.  
  5. use strict;
  6. use vars qw($VERSION @ISA $PROTO $DEBUG);
  7.  
  8. $VERSION = sprintf("%d.%02d", q$Revision: 1.36 $ =~ /(\d+)\.(\d+)/);
  9.  
  10. use IO::Socket qw(AF_INET INADDR_ANY inet_ntoa);
  11. @ISA=qw(IO::Socket::INET);
  12.  
  13. $PROTO = "HTTP/1.1";
  14.  
  15.  
  16. sub new
  17. {
  18.     my($class, %args) = @_;
  19.     $args{Listen} ||= 5;
  20.     $args{Proto}  ||= 'tcp';
  21.     return $class->SUPER::new(%args);
  22. }
  23.  
  24.  
  25. sub accept
  26. {
  27.     my $self = shift;
  28.     my $pkg = shift || "HTTP::Daemon::ClientConn";
  29.     my ($sock, $peer) = $self->SUPER::accept($pkg);
  30.     if ($sock) {
  31.         ${*$sock}{'httpd_daemon'} = $self;
  32.         return wantarray ? ($sock, $peer) : $sock;
  33.     }
  34.     else {
  35.         return;
  36.     }
  37. }
  38.  
  39.  
  40. sub url
  41. {
  42.     my $self = shift;
  43.     my $url = $self->_default_scheme . "://";
  44.     my $addr = $self->sockaddr;
  45.     if (!$addr || $addr eq INADDR_ANY) {
  46.      require Sys::Hostname;
  47.      $url .= lc Sys::Hostname::hostname();
  48.     }
  49.     else {
  50.     $url .= gethostbyaddr($addr, AF_INET) || inet_ntoa($addr);
  51.     }
  52.     my $port = $self->sockport;
  53.     $url .= ":$port" if $port != $self->_default_port;
  54.     $url .= "/";
  55.     $url;
  56. }
  57.  
  58.  
  59. sub _default_port {
  60.     80;
  61. }
  62.  
  63.  
  64. sub _default_scheme {
  65.     "http";
  66. }
  67.  
  68.  
  69. sub product_tokens
  70. {
  71.     "libwww-perl-daemon/$HTTP::Daemon::VERSION";
  72. }
  73.  
  74.  
  75.  
  76. package HTTP::Daemon::ClientConn;
  77.  
  78. use vars qw(@ISA $DEBUG);
  79. use IO::Socket ();
  80. @ISA=qw(IO::Socket::INET);
  81. *DEBUG = \$HTTP::Daemon::DEBUG;
  82.  
  83. use HTTP::Request  ();
  84. use HTTP::Response ();
  85. use HTTP::Status;
  86. use HTTP::Date qw(time2str);
  87. use LWP::MediaTypes qw(guess_media_type);
  88. use Carp ();
  89.  
  90. my $CRLF = "\015\012";   # "\r\n" is not portable
  91. my $HTTP_1_0 = _http_version("HTTP/1.0");
  92. my $HTTP_1_1 = _http_version("HTTP/1.1");
  93.  
  94.  
  95. sub get_request
  96. {
  97.     my($self, $only_headers) = @_;
  98.     if (${*$self}{'httpd_nomore'}) {
  99.         $self->reason("No more requests from this connection");
  100.     return;
  101.     }
  102.  
  103.     $self->reason("");
  104.     my $buf = ${*$self}{'httpd_rbuf'};
  105.     $buf = "" unless defined $buf;
  106.  
  107.     my $timeout = $ {*$self}{'io_socket_timeout'};
  108.     my $fdset = "";
  109.     vec($fdset, $self->fileno, 1) = 1;
  110.     local($_);
  111.  
  112.   READ_HEADER:
  113.     while (1) {
  114.     # loop until we have the whole header in $buf
  115.     $buf =~ s/^(?:\015?\012)+//;  # ignore leading blank lines
  116.     if ($buf =~ /\012/) {  # potential, has at least one line
  117.         if ($buf =~ /^\w+[^\012]+HTTP\/\d+\.\d+\015?\012/) {
  118.         if ($buf =~ /\015?\012\015?\012/) {
  119.             last READ_HEADER;  # we have it
  120.         }
  121.         elsif (length($buf) > 16*1024) {
  122.             $self->send_error(413); # REQUEST_ENTITY_TOO_LARGE
  123.             $self->reason("Very long header");
  124.             return;
  125.         }
  126.         }
  127.         else {
  128.         last READ_HEADER;  # HTTP/0.9 client
  129.         }
  130.     }
  131.     elsif (length($buf) > 16*1024) {
  132.         $self->send_error(414); # REQUEST_URI_TOO_LARGE
  133.         $self->reason("Very long first line");
  134.         return;
  135.     }
  136.     print STDERR "Need more data for complete header\n" if $DEBUG;
  137.     return unless $self->_need_more($buf, $timeout, $fdset);
  138.     }
  139.     if ($buf !~ s/^(\S+)[ \t]+(\S+)(?:[ \t]+(HTTP\/\d+\.\d+))?[^\012]*\012//) {
  140.     ${*$self}{'httpd_client_proto'} = _http_version("HTTP/1.0");
  141.     $self->send_error(400);  # BAD_REQUEST
  142.     $self->reason("Bad request line: $buf");
  143.     return;
  144.     }
  145.     my $method = $1;
  146.     my $uri = $2;
  147.     my $proto = $3 || "HTTP/0.9";
  148.     $uri = "http://$uri" if $method eq "CONNECT";
  149.     $uri = $HTTP::URI_CLASS->new($uri, $self->daemon->url);
  150.     my $r = HTTP::Request->new($method, $uri);
  151.     $r->protocol($proto);
  152.     ${*$self}{'httpd_client_proto'} = $proto = _http_version($proto);
  153.  
  154.     if ($proto >= $HTTP_1_0) {
  155.     # we expect to find some headers
  156.     my($key, $val);
  157.       HEADER:
  158.     while ($buf =~ s/^([^\012]*)\012//) {
  159.         $_ = $1;
  160.         s/\015$//;
  161.         if (/^([^:\s]+)\s*:\s*(.*)/) {
  162.         $r->push_header($key, $val) if $key;
  163.         ($key, $val) = ($1, $2);
  164.         }
  165.         elsif (/^\s+(.*)/) {
  166.         $val .= " $1";
  167.         }
  168.         else {
  169.         last HEADER;
  170.         }
  171.     }
  172.     $r->push_header($key, $val) if $key;
  173.     }
  174.  
  175.     my $conn = $r->header('Connection');
  176.     if ($proto >= $HTTP_1_1) {
  177.     ${*$self}{'httpd_nomore'}++ if $conn && lc($conn) =~ /\bclose\b/;
  178.     }
  179.     else {
  180.     ${*$self}{'httpd_nomore'}++ unless $conn &&
  181.                                            lc($conn) =~ /\bkeep-alive\b/;
  182.     }
  183.  
  184.     if ($only_headers) {
  185.     ${*$self}{'httpd_rbuf'} = $buf;
  186.         return $r;
  187.     }
  188.  
  189.     # Find out how much content to read
  190.     my $te  = $r->header('Transfer-Encoding');
  191.     my $ct  = $r->header('Content-Type');
  192.     my $len = $r->header('Content-Length');
  193.  
  194.     if ($te && lc($te) eq 'chunked') {
  195.     # Handle chunked transfer encoding
  196.     my $body = "";
  197.       CHUNK:
  198.     while (1) {
  199.         print STDERR "Chunked\n" if $DEBUG;
  200.         if ($buf =~ s/^([^\012]*)\012//) {
  201.         my $chunk_head = $1;
  202.         unless ($chunk_head =~ /^([0-9A-Fa-f]+)/) {
  203.             $self->send_error(400);
  204.             $self->reason("Bad chunk header $chunk_head");
  205.             return;
  206.         }
  207.         my $size = hex($1);
  208.         last CHUNK if $size == 0;
  209.  
  210.         my $missing = $size - length($buf) + 2; # 2=CRLF at chunk end
  211.         # must read until we have a complete chunk
  212.         while ($missing > 0) {
  213.             print STDERR "Need $missing more bytes\n" if $DEBUG;
  214.             my $n = $self->_need_more($buf, $timeout, $fdset);
  215.             return unless $n;
  216.             $missing -= $n;
  217.         }
  218.         $body .= substr($buf, 0, $size);
  219.         substr($buf, 0, $size+2) = '';
  220.  
  221.         }
  222.         else {
  223.         # need more data in order to have a complete chunk header
  224.         return unless $self->_need_more($buf, $timeout, $fdset);
  225.         }
  226.     }
  227.     $r->content($body);
  228.  
  229.     # pretend it was a normal entity body
  230.     $r->remove_header('Transfer-Encoding');
  231.     $r->header('Content-Length', length($body));
  232.  
  233.     my($key, $val);
  234.       FOOTER:
  235.     while (1) {
  236.         if ($buf !~ /\012/) {
  237.         # need at least one line to look at
  238.         return unless $self->_need_more($buf, $timeout, $fdset);
  239.         }
  240.         else {
  241.         $buf =~ s/^([^\012]*)\012//;
  242.         $_ = $1;
  243.         s/\015$//;
  244.         if (/^([\w\-]+)\s*:\s*(.*)/) {
  245.             $r->push_header($key, $val) if $key;
  246.             ($key, $val) = ($1, $2);
  247.         }
  248.         elsif (/^\s+(.*)/) {
  249.             $val .= " $1";
  250.         }
  251.         elsif (!length) {
  252.             last FOOTER;
  253.         }
  254.         else {
  255.             $self->reason("Bad footer syntax");
  256.             return;
  257.         }
  258.         }
  259.     }
  260.     $r->push_header($key, $val) if $key;
  261.  
  262.     }
  263.     elsif ($te) {
  264.     $self->send_error(501);     # Unknown transfer encoding
  265.     $self->reason("Unknown transfer encoding '$te'");
  266.     return;
  267.  
  268.     }
  269.     elsif ($ct && lc($ct) =~ m/^multipart\/\w+\s*;.*boundary\s*=\s*(\w+)/) {
  270.     # Handle multipart content type
  271.     my $boundary = "$CRLF--$1--$CRLF";
  272.     my $index;
  273.     while (1) {
  274.         $index = index($buf, $boundary);
  275.         last if $index >= 0;
  276.         # end marker not yet found
  277.         return unless $self->_need_more($buf, $timeout, $fdset);
  278.     }
  279.     $index += length($boundary);
  280.     $r->content(substr($buf, 0, $index));
  281.     substr($buf, 0, $index) = '';
  282.  
  283.     }
  284.     elsif ($len) {
  285.     # Plain body specified by "Content-Length"
  286.     my $missing = $len - length($buf);
  287.     while ($missing > 0) {
  288.         print "Need $missing more bytes of content\n" if $DEBUG;
  289.         my $n = $self->_need_more($buf, $timeout, $fdset);
  290.         return unless $n;
  291.         $missing -= $n;
  292.     }
  293.     if (length($buf) > $len) {
  294.         $r->content(substr($buf,0,$len));
  295.         substr($buf, 0, $len) = '';
  296.     }
  297.     else {
  298.         $r->content($buf);
  299.         $buf='';
  300.     }
  301.     }
  302.     ${*$self}{'httpd_rbuf'} = $buf;
  303.  
  304.     $r;
  305. }
  306.  
  307.  
  308. sub _need_more
  309. {
  310.     my $self = shift;
  311.     #my($buf,$timeout,$fdset) = @_;
  312.     if ($_[1]) {
  313.     my($timeout, $fdset) = @_[1,2];
  314.     print STDERR "select(,,,$timeout)\n" if $DEBUG;
  315.     my $n = select($fdset,undef,undef,$timeout);
  316.     unless ($n) {
  317.         $self->reason(defined($n) ? "Timeout" : "select: $!");
  318.         return;
  319.     }
  320.     }
  321.     print STDERR "sysread()\n" if $DEBUG;
  322.     my $n = sysread($self, $_[0], 2048, length($_[0]));
  323.     $self->reason(defined($n) ? "Client closed" : "sysread: $!") unless $n;
  324.     $n;
  325. }
  326.  
  327.  
  328. sub read_buffer
  329. {
  330.     my $self = shift;
  331.     my $old = ${*$self}{'httpd_rbuf'};
  332.     if (@_) {
  333.     ${*$self}{'httpd_rbuf'} = shift;
  334.     }
  335.     $old;
  336. }
  337.  
  338.  
  339. sub reason
  340. {
  341.     my $self = shift;
  342.     my $old = ${*$self}{'httpd_reason'};
  343.     if (@_) {
  344.         ${*$self}{'httpd_reason'} = shift;
  345.     }
  346.     $old;
  347. }
  348.  
  349.  
  350. sub proto_ge
  351. {
  352.     my $self = shift;
  353.     ${*$self}{'httpd_client_proto'} >= _http_version(shift);
  354. }
  355.  
  356.  
  357. sub _http_version
  358. {
  359.     local($_) = shift;
  360.     return 0 unless m,^(?:HTTP/)?(\d+)\.(\d+)$,i;
  361.     $1 * 1000 + $2;
  362. }
  363.  
  364.  
  365. sub antique_client
  366. {
  367.     my $self = shift;
  368.     ${*$self}{'httpd_client_proto'} < $HTTP_1_0;
  369. }
  370.  
  371.  
  372. sub force_last_request
  373. {
  374.     my $self = shift;
  375.     ${*$self}{'httpd_nomore'}++;
  376. }
  377.  
  378.  
  379. sub send_status_line
  380. {
  381.     my($self, $status, $message, $proto) = @_;
  382.     return if $self->antique_client;
  383.     $status  ||= RC_OK;
  384.     $message ||= status_message($status) || "";
  385.     $proto   ||= $HTTP::Daemon::PROTO || "HTTP/1.1";
  386.     print $self "$proto $status $message$CRLF";
  387. }
  388.  
  389.  
  390. sub send_crlf
  391. {
  392.     my $self = shift;
  393.     print $self $CRLF;
  394. }
  395.  
  396.  
  397. sub send_basic_header
  398. {
  399.     my $self = shift;
  400.     return if $self->antique_client;
  401.     $self->send_status_line(@_);
  402.     print $self "Date: ", time2str(time), $CRLF;
  403.     my $product = $self->daemon->product_tokens;
  404.     print $self "Server: $product$CRLF" if $product;
  405. }
  406.  
  407.  
  408. sub send_response
  409. {
  410.     my $self = shift;
  411.     my $res = shift;
  412.     if (!ref $res) {
  413.     $res ||= RC_OK;
  414.     $res = HTTP::Response->new($res, @_);
  415.     }
  416.     my $content = $res->content;
  417.     my $chunked;
  418.     unless ($self->antique_client) {
  419.     my $code = $res->code;
  420.     $self->send_basic_header($code, $res->message, $res->protocol);
  421.     if ($code =~ /^(1\d\d|[23]04)$/) {
  422.         # make sure content is empty
  423.         $res->remove_header("Content-Length");
  424.         $content = "";
  425.     }
  426.     elsif ($res->request && $res->request->method eq "HEAD") {
  427.         # probably OK
  428.     }
  429.     elsif (ref($content) eq "CODE") {
  430.         if ($self->proto_ge("HTTP/1.1")) {
  431.         $res->push_header("Transfer-Encoding" => "chunked");
  432.         $chunked++;
  433.         }
  434.         else {
  435.         $self->force_last_request;
  436.         }
  437.     }
  438.     elsif (length($content)) {
  439.         $res->header("Content-Length" => length($content));
  440.     }
  441.     else {
  442.         $self->force_last_request;
  443.     }
  444.     print $self $res->headers_as_string($CRLF);
  445.     print $self $CRLF;  # separates headers and content
  446.     }
  447.     if (ref($content) eq "CODE") {
  448.     while (1) {
  449.         my $chunk = &$content();
  450.         last unless defined($chunk) && length($chunk);
  451.         if ($chunked) {
  452.         printf $self "%x%s%s%s", length($chunk), $CRLF, $chunk, $CRLF;
  453.         }
  454.         else {
  455.         print $self $chunk;
  456.         }
  457.     }
  458.     print $self "0$CRLF$CRLF" if $chunked;  # no trailers either
  459.     }
  460.     elsif (length $content) {
  461.     print $self $content;
  462.     }
  463. }
  464.  
  465.  
  466. sub send_redirect
  467. {
  468.     my($self, $loc, $status, $content) = @_;
  469.     $status ||= RC_MOVED_PERMANENTLY;
  470.     Carp::croak("Status '$status' is not redirect") unless is_redirect($status);
  471.     $self->send_basic_header($status);
  472.     my $base = $self->daemon->url;
  473.     $loc = $HTTP::URI_CLASS->new($loc, $base) unless ref($loc);
  474.     $loc = $loc->abs($base);
  475.     print $self "Location: $loc$CRLF";
  476.     if ($content) {
  477.     my $ct = $content =~ /^\s*</ ? "text/html" : "text/plain";
  478.     print $self "Content-Type: $ct$CRLF";
  479.     }
  480.     print $self $CRLF;
  481.     print $self $content if $content;
  482.     $self->force_last_request;  # no use keeping the connection open
  483. }
  484.  
  485.  
  486. sub send_error
  487. {
  488.     my($self, $status, $error) = @_;
  489.     $status ||= RC_BAD_REQUEST;
  490.     Carp::croak("Status '$status' is not an error") unless is_error($status);
  491.     my $mess = status_message($status);
  492.     $error  ||= "";
  493.     $mess = <<EOT;
  494. <title>$status $mess</title>
  495. <h1>$status $mess</h1>
  496. $error
  497. EOT
  498.     unless ($self->antique_client) {
  499.         $self->send_basic_header($status);
  500.         print $self "Content-Type: text/html$CRLF";
  501.     print $self "Content-Length: " . length($mess) . $CRLF;
  502.         print $self $CRLF;
  503.     }
  504.     print $self $mess;
  505.     $status;
  506. }
  507.  
  508.  
  509. sub send_file_response
  510. {
  511.     my($self, $file) = @_;
  512.     if (-d $file) {
  513.     $self->send_dir($file);
  514.     }
  515.     elsif (-f _) {
  516.     # plain file
  517.     local(*F);
  518.     sysopen(F, $file, 0) or 
  519.       return $self->send_error(RC_FORBIDDEN);
  520.     binmode(F);
  521.     my($ct,$ce) = guess_media_type($file);
  522.     my($size,$mtime) = (stat _)[7,9];
  523.     unless ($self->antique_client) {
  524.         $self->send_basic_header;
  525.         print $self "Content-Type: $ct$CRLF";
  526.         print $self "Content-Encoding: $ce$CRLF" if $ce;
  527.         print $self "Content-Length: $size$CRLF" if $size;
  528.         print $self "Last-Modified: ", time2str($mtime), "$CRLF" if $mtime;
  529.         print $self $CRLF;
  530.     }
  531.     $self->send_file(\*F);
  532.     return RC_OK;
  533.     }
  534.     else {
  535.     $self->send_error(RC_NOT_FOUND);
  536.     }
  537. }
  538.  
  539.  
  540. sub send_dir
  541. {
  542.     my($self, $dir) = @_;
  543.     $self->send_error(RC_NOT_FOUND) unless -d $dir;
  544.     $self->send_error(RC_NOT_IMPLEMENTED);
  545. }
  546.  
  547.  
  548. sub send_file
  549. {
  550.     my($self, $file) = @_;
  551.     my $opened = 0;
  552.     local(*FILE);
  553.     if (!ref($file)) {
  554.     open(FILE, $file) || return undef;
  555.     binmode(FILE);
  556.     $file = \*FILE;
  557.     $opened++;
  558.     }
  559.     my $cnt = 0;
  560.     my $buf = "";
  561.     my $n;
  562.     while ($n = sysread($file, $buf, 8*1024)) {
  563.     last if !$n;
  564.     $cnt += $n;
  565.     print $self $buf;
  566.     }
  567.     close($file) if $opened;
  568.     $cnt;
  569. }
  570.  
  571.  
  572. sub daemon
  573. {
  574.     my $self = shift;
  575.     ${*$self}{'httpd_daemon'};
  576. }
  577.  
  578.  
  579. 1;
  580.  
  581. __END__
  582.  
  583. =head1 NAME
  584.  
  585. HTTP::Daemon - a simple http server class
  586.  
  587. =head1 SYNOPSIS
  588.  
  589.   use HTTP::Daemon;
  590.   use HTTP::Status;
  591.  
  592.   my $d = HTTP::Daemon->new || die;
  593.   print "Please contact me at: <URL:", $d->url, ">\n";
  594.   while (my $c = $d->accept) {
  595.       while (my $r = $c->get_request) {
  596.       if ($r->method eq 'GET' and $r->url->path eq "/xyzzy") {
  597.               # remember, this is *not* recommended practice :-)
  598.           $c->send_file_response("/etc/passwd");
  599.       }
  600.       else {
  601.           $c->send_error(RC_FORBIDDEN)
  602.       }
  603.       }
  604.       $c->close;
  605.       undef($c);
  606.   }
  607.  
  608. =head1 DESCRIPTION
  609.  
  610. Instances of the C<HTTP::Daemon> class are HTTP/1.1 servers that
  611. listen on a socket for incoming requests. The C<HTTP::Daemon> is a
  612. subclass of C<IO::Socket::INET>, so you can perform socket operations
  613. directly on it too.
  614.  
  615. The accept() method will return when a connection from a client is
  616. available.  The returned value will be an C<HTTP::Daemon::ClientConn>
  617. object which is another C<IO::Socket::INET> subclass.  Calling the
  618. get_request() method on this object will read data from the client and
  619. return an C<HTTP::Request> object.  The ClientConn object also provide
  620. methods to send back various responses.
  621.  
  622. This HTTP daemon does not fork(2) for you.  Your application, i.e. the
  623. user of the C<HTTP::Daemon> is responsible for forking if that is
  624. desirable.  Also note that the user is responsible for generating
  625. responses that conform to the HTTP/1.1 protocol.
  626.  
  627. The following methods of C<HTTP::Daemon> are new (or enhanced) relative
  628. to the C<IO::Socket::INET> base class:
  629.  
  630. =over 4
  631.  
  632. =item $d = HTTP::Daemon->new
  633.  
  634. =item $d = HTTP::Daemon->new( %opts )
  635.  
  636. The constructor method takes the same arguments as the
  637. C<IO::Socket::INET> constructor, but unlike its base class it can also
  638. be called without any arguments.  The daemon will then set up a listen
  639. queue of 5 connections and allocate some random port number.
  640.  
  641. A server that wants to bind to some specific address on the standard
  642. HTTP port will be constructed like this:
  643.  
  644.   $d = HTTP::Daemon->new(
  645.            LocalAddr => 'www.thisplace.com',
  646.            LocalPort => 80,
  647.        );
  648.  
  649. See L<IO::Socket::INET> for a description of other arguments that can
  650. be used configure the daemon during construction.
  651.  
  652. =item $c = $d->accept
  653.  
  654. =item $c = $d->accept( $pkg )
  655.  
  656. =item ($c, $peer_addr) = $d->accept
  657.  
  658. This method works the same the one provided by the base class, but it
  659. returns an C<HTTP::Daemon::ClientConn> reference by default.  If a
  660. package name is provided as argument, then the returned object will be
  661. blessed into the given class.  It is probably a good idea to make that
  662. class a subclass of C<HTTP::Daemon::ClientConn>.
  663.  
  664. The accept method will return C<undef> if timeouts have been enabled
  665. and no connection is made within the given time.  The timeout() method
  666. is described in L<IO::Socket>.
  667.  
  668. In list context both the client object and the peer address will be
  669. returned; see the description of the accept method L<IO::Socket> for
  670. details.
  671.  
  672. =item $d->url
  673.  
  674. Returns a URL string that can be used to access the server root.
  675.  
  676. =item $d->product_tokens
  677.  
  678. Returns the name that this server will use to identify itself.  This
  679. is the string that is sent with the C<Server> response header.  The
  680. main reason to have this method is that subclasses can override it if
  681. they want to use another product name.
  682.  
  683. The default is the string "libwww-perl-daemon/#.##" where "#.##" is
  684. replaced with the version number of this module.
  685.  
  686. =back
  687.  
  688. The C<HTTP::Daemon::ClientConn> is a C<IO::Socket::INET>
  689. subclass. Instances of this class are returned by the accept() method
  690. of C<HTTP::Daemon>.  The following methods are provided:
  691.  
  692. =over 4
  693.  
  694. =item $c->get_request
  695.  
  696. =item $c->get_request( $headers_only )
  697.  
  698. This method read data from the client and turns it into an
  699. C<HTTP::Request> object which is returned.  It returns C<undef>
  700. if reading fails.  If it fails, then the C<HTTP::Daemon::ClientConn>
  701. object ($c) should be discarded, and you should not try call this
  702. method again on it.  The $c->reason method might give you some
  703. information about why $c->get_request failed.
  704.  
  705. The get_request() method will normally not return until the whole
  706. request has been received from the client.  This might not be what you
  707. want if the request is an upload of a large file (and with chunked
  708. transfer encoding HTTP can even support infinite request messages -
  709. uploading live audio for instance).  If you pass a TRUE value as the
  710. $headers_only argument, then get_request() will return immediately
  711. after parsing the request headers and you are responsible for reading
  712. the rest of the request content.  If you are going to call
  713. $c->get_request again on the same connection you better read the
  714. correct number of bytes.
  715.  
  716. =item $c->read_buffer
  717.  
  718. =item $c->read_buffer( $new_value )
  719.  
  720. Bytes read by $c->get_request, but not used are placed in the I<read
  721. buffer>.  The next time $c->get_request is called it will consume the
  722. bytes in this buffer before reading more data from the network
  723. connection itself.  The read buffer is invalid after $c->get_request
  724. has failed.
  725.  
  726. If you handle the reading of the request content yourself you need to
  727. empty this buffer before you read more and you need to place
  728. unconsumed bytes here.  You also need this buffer if you implement
  729. services like I<101 Switching Protocols>.
  730.  
  731. This method always return the old buffer content and can optionally
  732. replace the buffer content if you pass it an argument.
  733.  
  734. =item $c->reason
  735.  
  736. When $c->get_request returns C<undef> you can obtain a short string
  737. describing why it happened by calling $c->reason.
  738.  
  739. =item $c->proto_ge( $proto )
  740.  
  741. Return TRUE if the client announced a protocol with version number
  742. greater or equal to the given argument.  The $proto argument can be a
  743. string like "HTTP/1.1" or just "1.1".
  744.  
  745. =item $c->antique_client
  746.  
  747. Return TRUE if the client speaks the HTTP/0.9 protocol.  No status
  748. code and no headers should be returned to such a client.  This should
  749. be the same as !$c->proto_ge("HTTP/1.0").
  750.  
  751. =item $c->force_last_request
  752.  
  753. Make sure that $c->get_request will not try to read more requests off
  754. this connection.  If you generate a response that is not self
  755. delimiting, then you should signal this fact by calling this method.
  756.  
  757. This attribute is turned on automatically if the client announces
  758. protocol HTTP/1.0 or worse and does not include a "Connection:
  759. Keep-Alive" header.  It is also turned on automatically when HTTP/1.1
  760. or better clients send the "Connection: close" request header.
  761.  
  762. =item $c->send_status_line
  763.  
  764. =item $c->send_status_line( $code )
  765.  
  766. =item $c->send_status_line( $code, $mess )
  767.  
  768. =item $c->send_status_line( $code, $mess, $proto )
  769.  
  770. Send the status line back to the client.  If $code is omitted 200 is
  771. assumed.  If $mess is omitted, then a message corresponding to $code
  772. is inserted.  If $proto is missing the content of the
  773. $HTTP::Daemon::PROTO variable is used.
  774.  
  775. =item $c->send_crlf
  776.  
  777. Send the CRLF sequence to the client.
  778.  
  779. =item $c->send_basic_header
  780.  
  781. =item $c->send_basic_header( $code )
  782.  
  783. =item $c->send_basic_header( $code, $mess )
  784.  
  785. =item $c->send_basic_header( $code, $mess, $proto )
  786.  
  787. Send the status line and the "Date:" and "Server:" headers back to
  788. the client.  This header is assumed to be continued and does not end
  789. with an empty CRLF line.
  790.  
  791. See the description of send_status_line() for the description of the
  792. accepted arguments.
  793.  
  794. =item $c->send_response( $res )
  795.  
  796. Write a C<HTTP::Response> object to the
  797. client as a response.  We try hard to make sure that the response is
  798. self delimiting so that the connection can stay persistent for further
  799. request/response exchanges.
  800.  
  801. The content attribute of the C<HTTP::Response> object can be a normal
  802. string or a subroutine reference.  If it is a subroutine, then
  803. whatever this callback routine returns is written back to the
  804. client as the response content.  The routine will be called until it
  805. return an undefined or empty value.  If the client is HTTP/1.1 aware
  806. then we will use chunked transfer encoding for the response.
  807.  
  808. =item $c->send_redirect( $loc )
  809.  
  810. =item $c->send_redirect( $loc, $code )
  811.  
  812. =item $c->send_redirect( $loc, $code, $entity_body )
  813.  
  814. Send a redirect response back to the client.  The location ($loc) can
  815. be an absolute or relative URL. The $code must be one the redirect
  816. status codes, and defaults to "301 Moved Permanently"
  817.  
  818. =item $c->send_error
  819.  
  820. =item $c->send_error( $code )
  821.  
  822. =item $c->send_error( $code, $error_message )
  823.  
  824. Send an error response back to the client.  If the $code is missing a
  825. "Bad Request" error is reported.  The $error_message is a string that
  826. is incorporated in the body of the HTML entity body.
  827.  
  828. =item $c->send_file_response( $filename )
  829.  
  830. Send back a response with the specified $filename as content.  If the
  831. file is a directory we try to generate an HTML index of it.
  832.  
  833. =item $c->send_file( $filename )
  834.  
  835. =item $c->send_file( $fd )
  836.  
  837. Copy the file to the client.  The file can be a string (which
  838. will be interpreted as a filename) or a reference to an C<IO::Handle>
  839. or glob.
  840.  
  841. =item $c->daemon
  842.  
  843. Return a reference to the corresponding C<HTTP::Daemon> object.
  844.  
  845. =back
  846.  
  847. =head1 SEE ALSO
  848.  
  849. RFC 2616
  850.  
  851. L<IO::Socket::INET>, L<IO::Socket>
  852.  
  853. =head1 COPYRIGHT
  854.  
  855. Copyright 1996-2003, Gisle Aas
  856.  
  857. This library is free software; you can redistribute it and/or
  858. modify it under the same terms as Perl itself.
  859.  
  860.